package com.zhangzeqin.code.dao.Impl;

import com.zhangzeqin.code.dao.GetWordDao;
import com.zhangzeqin.code.dao.WordBookDao;
import com.zhangzeqin.code.util.DruidTool;
import com.zhangzeqin.code.util.JdbcUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordBookDaoImpl implements WordBookDao , GetWordDao {


    @Override
    public boolean addBook(String bookName,String nature) {
        String sql="create table "+ nature +" (word varchar(20),meaning varchar(20))";
        String sql1="insert into books values(?,?);";
        //连接数据库
        boolean flag=false;
        int row=0;
        try {
            flag = JdbcUtil.creatTbale(sql);
            row=JdbcUtil.executeUpdate(sql1,nature,bookName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(flag==true&&row!=0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteBook(String bookName) throws SQLException {
        //删除books表里的数据,bookName相当于books里的nature
        String sql="delete from books where nature = ?;";
        //删除单词书表
        String sql1="drop table "+bookName+";";
        //连接数据库
        int row=0;
        int row1=0;

        row=JdbcUtil.executeUpdate(sql1);
        row1=JdbcUtil.executeUpdate(sql,bookName);

        if(row!=-1&&row1!=-1){
            return true;
        }
        return false;
    }

    @Override
    public boolean checkWord(String wordBook,String word, String meaning) {
       String sql="select * from ? where word=? or meaning=?;";
       boolean flag=false;
        try {
            flag=JdbcUtil.Query(sql,wordBook,word,meaning);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }


    @Override
    public List<Map> getWord(String table) throws SQLException {
        String sql="select  * from "+table+";";
        //获取连接
        Connection connection = DruidTool.getConnection();

        //创建预编译statement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //占位符赋值,设置?的值
        //preparedStatement.setObject(1,table);
        //创建一个存放map型的集合
        List<Map> list=new ArrayList<>();

        //返回结果集
        ResultSet resultSet = preparedStatement.executeQuery();
//        //获取列的信息对象(可以获取列的名称,根据小角标,获取列的数量)
        ResultSetMetaData metaData=resultSet.getMetaData();
        int columnCount=metaData.getColumnCount();
        while(resultSet.next()){
            Map map=new HashMap();
            //获取指定列下角标的值
            Object key = resultSet.getObject(1);
            Object value = resultSet.getObject(2);
            //将每行数据存到map中
            map.put(key,value);
            //将map存到集合中
            list.add(map);
        }

        if (resultSet.next()){
            //是否收回连接,需要考虑是不是事务
            if(connection.getAutoCommit()){
                //没有开启事务,正常回收连接
                DruidTool.recycleConnection(connection);
            }
            resultSet.close();

        }
        return list;
    }

}
